import { Internet } from "./internet";
const { shell } = require('electron')
const ipc = require('electron').ipcRenderer;

const checkOnline = () => {
    return navigator.onLine;
}

const fullScreen = () => {
    ipc.send('window-full-screen');
}

const closeAllWindow = () => {
    ipc.send('closeWindow');
    ipc.send('close-all-window');
    ipc.send('window-close');
}

const miniWindow = () => {
    ipc.send('window-restore-size');
}

const minimise = () => {
    ipc.send('window-minimize');
}

const closeResourceWindow = () => {
    ipc.send('close-resource-window');
}

const openNativeFile = (filePath) => {
    shell.openItem(filePath)
}

const openExternal = (url) => {
    shell.openExternal(url);
}

const checkInternet = (callback) => {
    Internet.checkInternetByInvokeApi(callback);
}

const executeCmd = (cmdStr, cmdPath) => {
    const exec = require('child_process').exec
    let workerProcess = exec(cmdStr, { cwd: cmdPath })
    // 打印正常的后台可执行程序输出
    workerProcess.stdout.on('data', function (data) {
        console.log('stdout: ' + data)
    })
    // 打印错误的后台可执行程序输出
    workerProcess.stderr.on('data', function (data) {
        console.log('stderr: ' + data)
    })
    // 退出之后的输出
    workerProcess.on('close', function (code) {
        console.log('out code：' + code)
    })
}

const executeBatCmd = (cmdBatPath) => {
    const exec = require('child_process').exec
    exec(cmdBatPath, (err, stdout, stderr) => {
        if (err) {
            console.error(err);
            return;
        }
        console.log(stdout);
    });
}

const executeExe = (exeName, args) => {
    const { spawn } = require('child_process');
    // option:detached, true-独立于当前进程以外运行
    spawn(exeName, args, { detached: true });
}

/**
 * 打开PPT
 * pptPath: string, PPT文件的绝对路径
 * isSlideShow: bool, 是否使用播放模式打开，true为使用
 * add by jjli, 20200714
 */
const openPPT = (pptPath, isSlideShow=false) => {
    // 读取注册表，获取PPT的安装路径
    readRegVaule('HKLM\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\App Paths\\powerpnt.exe', function (error, values) {
        if (error) {
            // 注册表读取失败，使用系统默认方式打开PPT
            openNativeFile(pptPath);
        }
        if (values) {
            try {
                let version = values.Path.value.match(/[0-9]+\\$/)[0]
                if(parseInt(version)<10){
                    openNativeFile(pptPath);
                    return
                }
                // 获取POWERPNT.exe的路径
                let powerPntPath = values.Path.value + '\\POWERPNT.EXE';
                let args = [pptPath];
                if (isSlideShow) {
                    args = ['/s', pptPath];
                }
                executeExe(powerPntPath, args);
            } catch (error) {
                // 打开失败，使用系统默认方式打开PPT
                openNativeFile(pptPath);
            }
        }
        else {
            // 打开失败，使用系统默认方式打开PPT
            openNativeFile(pptPath);
        }
    })
}

const readRegVaule = (regPath, callback) => {
    let err = null;
    const registry = require('regedit');
    registry.list(regPath, (error, result) => {
        if (error) {
            if (callback) {
                err = error
                callback(err)
            }
        }
        if (result) {
            let values = result[regPath].values;
            callback(err, values);
        }
    })
}

export default {
    checkOnline,
    fullScreen,
    closeAllWindow,
    closeResourceWindow,
    openNativeFile,
    openExternal,
    checkInternet,
    executeCmd,
    executeBatCmd,
    executeExe,
    openPPT,
    readRegVaule,
    miniWindow,
    minimise
}
